package com.zhuoyue.biz.core.command.impl;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhuoyue.biz.core.command.Device;
import com.zhuoyue.biz.core.command.constant.ZhiNengChangJingMianBan0Faa8A04CmdDeal;
import com.zhuoyue.common.utils.CmdUtils;
import org.apache.commons.lang3.StringUtils;


/**
 * @author Administrator
 */
public class ZhiNengChangJingMianBan0Faa8A04 implements Device {

    @Override
    public String getCommand(String controlParam) {

        Map<String, String> map = (Map) JSONObject.parse(controlParam);

        String controlType = map.get("controlType");
        if (StringUtils.isEmpty(controlType)) {
            return null;
        }

        String cmd = "";

        if (ZhiNengChangJingMianBan0Faa8A04CmdDeal.ControlType.SCENE_NUMBER_SET.equals(controlType)) {
            cmd = getSceneNumberSetCmd(map);
        } else if (ZhiNengChangJingMianBan0Faa8A04CmdDeal.ControlType.SCENE_NUMBER_GET.equals(controlType)) {
            cmd = getSceneNumberGetCmd(map);
        }
        return cmd;
    }

    /**
     * 获取场景编号配置命令
     *
     * @param map
     * @return
     */
    private String getSceneNumberSetCmd(Map<String, String> map) {

        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));

        String cmd3To13 = ZhiNengChangJingMianBan0Faa8A04CmdDeal.SceneNumberSet.CMD_3 +
                ZhiNengChangJingMianBan0Faa8A04CmdDeal.SceneNumberSet.CMD_4 +
                map.get("gatewayMac") +
                map.get("deviceMac") +
                ZhiNengChangJingMianBan0Faa8A04CmdDeal.SceneNumberSet.CMD_7 +
                CmdUtils.intToHexTwo(Integer.parseInt(actionsMap.get("wayNumber"))) +
                ZhiNengChangJingMianBan0Faa8A04CmdDeal.SceneNumberSet.CMD_9 +
                CmdUtils.intToHexTwo(Integer.parseInt(actionsMap.get("sceneNumber"))) +
                ZhiNengChangJingMianBan0Faa8A04CmdDeal.SceneNumberSet.CMD_11 +
                CmdUtils.intToHexTwo(Integer.parseInt(actionsMap.get("sceneName"))) +
                ZhiNengChangJingMianBan0Faa8A04CmdDeal.SceneNumberSet.CMD_13;

        String cmd2 = CmdUtils.getDateLength(cmd3To13);

        String cmd14 = CmdUtils.getCmdBodySum(cmd3To13);

        String fullCmd = ZhiNengChangJingMianBan0Faa8A04CmdDeal.SceneNumberSet.CMD_1 +
                cmd2 +
                cmd3To13 +
                cmd14 +
                ZhiNengChangJingMianBan0Faa8A04CmdDeal.SceneNumberSet.CMD_15;

        return fullCmd;
    }


    /**
     * 获取场景编号获取的命令
     *
     * @param map
     * @return
     */
    private String getSceneNumberGetCmd(Map<String, String> map) {

        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));

        String cmd8 = "";
        String wayNumber = actionsMap.get("wayNumber");
        switch (wayNumber) {
            case "1":
                cmd8 = ZhiNengChangJingMianBan0Faa8A04CmdDeal.SceneNumberGet.CMD_8_ONE;
                break;
            case "2":
                cmd8 = ZhiNengChangJingMianBan0Faa8A04CmdDeal.SceneNumberGet.CMD_8_TWO;
                break;
            case "3":
                cmd8 = ZhiNengChangJingMianBan0Faa8A04CmdDeal.SceneNumberGet.CMD_8_THREE;
                break;
            default:
                break;
        }


        String cmd3To8 = ZhiNengChangJingMianBan0Faa8A04CmdDeal.SceneNumberGet.CMD_3 +
                ZhiNengChangJingMianBan0Faa8A04CmdDeal.SceneNumberGet.CMD_4 +
                map.get("gatewayMac") +
                map.get("deviceMac") +
                ZhiNengChangJingMianBan0Faa8A04CmdDeal.SceneNumberGet.CMD_7 +
                cmd8;

        String cmd2 = CmdUtils.getDateLength(cmd3To8);

        String cmd9 = CmdUtils.getCmdBodySum(cmd3To8);

        String fullCmd = ZhiNengChangJingMianBan0Faa8A04CmdDeal.SceneNumberGet.CMD_1 +
                cmd2 +
                cmd3To8 +
                cmd9 +
                ZhiNengChangJingMianBan0Faa8A04CmdDeal.SceneNumberGet.CMD_10;

        return fullCmd;
    }


}
